{
  Formulario base para demos do projeto Peebles. Para exemplos simples de uso, ver projetos
  PeeblesTimer e PeeblesThrd.

  Desenvolvido por Anderson Carli (anderson.carli@gmail.com) e distribuido sob a licenca MIT.
  http://peebles.googlecode.com
}

unit PeeblesMainForm;

interface

uses
  Windows, Messages, Classes, Graphics, Controls, Forms, Peebles;

type

  TPeeblesMainForm = class(TForm)
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  private
  protected
    procedure Paint; override;
    procedure DoShow; override;
    procedure OnTimer(var Msg: TWMTimer); message WM_TIMER;
  public
    Jail: TJail;
    constructor Create(AOwner:TComponent); override;
    destructor Destroy; override;
    function AddPeeble(PeebleClass: TPeebleClass; x, y: Integer): TPeeble; virtual;
    procedure RemovePeeble(P: TPeeble); virtual;
  end;

implementation {$R *.dfm}

uses Math;

{ TPeeblesMainForm }
constructor TPeeblesMainForm.Create(AOwner:TComponent);
var
  R:TRect;
begin
  inherited;
  {Cria uma prisao retanglar}
  R := ClientRect;
  InflateRect(R, -10, -10);
  Jail := TJail.Create(R);
end;

destructor TPeeblesMainForm.Destroy;
begin
  KillTimer(Handle, 1);
  Jail.Free;
  inherited;
end;

procedure TPeeblesMainForm.DoShow;
begin
  inherited;
  {Ajusta o Timer para animar as Peeples e redesenhar o formulario a cada 40 ms}
  SetTimer(Handle, 1, 40, nil);
  DoubleBuffered := True;
end;

procedure TPeeblesMainForm.Paint;
begin
  inherited;
  Jail.Paint(Canvas);
end;

procedure TPeeblesMainForm.OnTimer(var Msg: TWMTimer);
begin
  Invalidate;
end;

function TPeeblesMainForm.AddPeeble(PeebleClass: TPeebleClass; x, y: Integer): TPeeble;
begin
  Result := Jail.NewPeeble(PeebleClass, x, y);
  if PeebleClass.InheritsFrom(TLivePeeble) then
    TLivePeeble(Result).Run;
end;

procedure TPeeblesMainForm.RemovePeeble(P: TPeeble);
begin
  Jail.Peebles.Remove(P);
end;

procedure TPeeblesMainForm.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  case Button of
    mbLeft:
      if ssShift in Shift then
        AddPeeble(TPeeble, x, y).Speed := Point(-3, -2)
      else
        AddPeeble(TPeeble, x, y);
    mbRight:
      if Jail.Peebles.Count>0 then
        RemovePeeble(Jail.Peebles.Last);
  end;
end;

end.
